package org.abc.fund.service.tools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * A utility class to consume an InputStream in a separate thread.
 * This is used to prevent the main thread from blocking while waiting for a process's output or error stream.
 */
public class StreamGobbler implements Runnable {
    private final InputStream inputStream;
    private final StringBuilder builder = new StringBuilder();

    public StreamGobbler(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public void run() {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {

            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line).append("\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getOutput() {
        return builder.toString();
    }
}







